package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.dto.ComplaintCategoryDO;
import com.triones.ljr.entity.dto.ComplaintDO;
import com.triones.ljr.entity.dto.PolicyOptionDataDO;
import com.triones.ljr.entity.vo.ComplaintCategoryVO;
import com.triones.ljr.entity.vo.ComplaintVO;
import com.triones.ljr.mapper.ComplaintCategoryMapper;
import com.triones.ljr.mapper.ComplaintMapper;
import com.triones.ljr.service.ComplaintService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 投诉建议
 */
@Service
@RequiredArgsConstructor
public class ComplaintServiceImpl implements ComplaintService {

    private final ComplaintMapper complaintMapper;

    private final ComplaintCategoryMapper complaintCategoryMapper;

    @Override
    public void add(ComplaintDO dto) {
        complaintMapper.insert(dto);
    }

    @Override
    public TableDataInfo<ComplaintVO> pageList(ComplaintDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<ComplaintDO> lqw = new LambdaQueryWrapper<ComplaintDO>();
        lqw.eq(ComplaintDO::getDelFlag, "0")
            .eq(StringUtils.isNotEmpty(dto.getType()),  ComplaintDO::getType, dto.getType())
            .like(StringUtils.isNotBlank(dto.getName()), ComplaintDO::getName, dto.getName()).or()
            .like(StringUtils.isNotBlank(dto.getMobile()), ComplaintDO::getMobile, dto.getMobile()).or()
            .like(StringUtils.isNotBlank(dto.getAddress()), ComplaintDO::getAddress, dto.getAddress()).or()
            .like(StringUtils.isNotBlank(dto.getTitle()), ComplaintDO::getTitle, dto.getTitle()).or()
            .like(StringUtils.isNotBlank(dto.getContent()), ComplaintDO::getContent, dto.getContent());
        Page<ComplaintVO> page = complaintMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public List<ComplaintVO> selectComplaintList(ComplaintDO dto) {
        LambdaQueryWrapper<ComplaintDO> lqw = new LambdaQueryWrapper<ComplaintDO>();
        lqw.eq(ComplaintDO::getDelFlag, "0")
            .like(StringUtils.isNotBlank(dto.getName()), ComplaintDO::getName, dto.getName()).or()
            .like(StringUtils.isNotBlank(dto.getMobile()), ComplaintDO::getMobile, dto.getMobile()).or()
            .like(StringUtils.isNotBlank(dto.getAddress()), ComplaintDO::getAddress, dto.getAddress()).or()
            .like(StringUtils.isNotBlank(dto.getTitle()), ComplaintDO::getTitle, dto.getTitle()).or()
            .like(StringUtils.isNotBlank(dto.getContent()), ComplaintDO::getContent, dto.getContent());
        return complaintMapper.selectVoList(lqw);
    }

    @Override
    public boolean deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return complaintMapper.deleteBatchIds(idList) > 0;
    }

    @Override
    public ComplaintVO getInfo(Long id) {
        return complaintMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<ComplaintCategoryVO> categoryPageList(ComplaintCategoryDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<ComplaintCategoryDO> lqw = new LambdaQueryWrapper<ComplaintCategoryDO>();
        lqw.eq(ComplaintCategoryDO::getIsDel, "0")
            .eq(StringUtils.isNotBlank(dto.getStatus()), ComplaintCategoryDO::getStatus, dto.getStatus())
            .like(StringUtils.isNotBlank(dto.getName()), ComplaintCategoryDO::getName, dto.getName())
            .orderByAsc(ComplaintCategoryDO::getSort);
        Page<ComplaintCategoryVO> page = complaintCategoryMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public boolean addCategory(ComplaintCategoryDO dto) {
        dto.setCreateTime(DateUtil.getDateTimeString());
        return complaintCategoryMapper.insert(dto) > 0;
    }

    @Override
    public boolean editCategory(ComplaintCategoryDO dto) {
        return complaintCategoryMapper.updateById(dto) > 0;
    }

    @Override
    public ComplaintCategoryVO getCategoryInfo(Long id) {
        return complaintCategoryMapper.selectVoById(id);
    }

    @Override
    public boolean removeCategory(Long id) {
        ComplaintCategoryDO complaintCategoryDO = complaintCategoryMapper.selectById(id);
        if (complaintCategoryDO == null) {
            throw new ServiceException("信息不存在");
        }
        complaintCategoryDO.setIsDel("1");
        return complaintCategoryMapper.updateById(complaintCategoryDO) > 0;
    }

    @Override
    public boolean checkCategoryNameUnique(ComplaintCategoryDO dto) {
        boolean exist = complaintCategoryMapper.exists(new LambdaQueryWrapper<ComplaintCategoryDO>()
            .eq(ComplaintCategoryDO::getName, dto.getName())
            .eq(ComplaintCategoryDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), ComplaintCategoryDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public List<ComplaintCategoryVO> getCategoryList() {
        return complaintCategoryMapper.selectVoList(new LambdaQueryWrapper<ComplaintCategoryDO>()
            .eq(ComplaintCategoryDO::getIsDel, "0")
            .eq(ComplaintCategoryDO::getStatus, "0")
            .orderByAsc(ComplaintCategoryDO::getSort));
    }

    @Override
    public boolean circulation(ComplaintDO dto) {
        ComplaintDO complaintDO = complaintMapper.selectById(dto.getId());
        if (complaintDO == null) {
            throw new ServiceException("投诉信息不存在");
        }
        complaintDO.setLeadDept(dto.getLeadDept());
        complaintDO.setAuditDept(dto.getAuditDept());
        complaintDO.setCirculationType(dto.getCirculationType());
        complaintDO.setTimeLimit(dto.getTimeLimit());
        return complaintMapper.updateById(complaintDO) > 0;
    }
}
